package com.jason.www.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.InputType;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.animation.Animation;
import android.view.animation.CycleInterpolator;
import android.view.animation.TranslateAnimation;

import com.jason.www.R;

import androidx.appcompat.widget.AppCompatEditText;
import androidx.core.content.ContextCompat;

/**
 * @author：Jason
 * @date: 2021/7/8 11:47
 * @email：1129847330@qq.com
 * @description: 带密码显示隐藏功能的密码输入框
 */
public class PassWordEditText extends AppCompatEditText {
    /**
     * 眼睛图标
     */
    private Drawable drawable;
    /**
     * 密码是否可见
     */
    private boolean isVisible;
    private Context mContext;

    public PassWordEditText(Context context) {
        this(context, null);
    }

    public PassWordEditText(Context context, AttributeSet attrs) {
        //这里构造方法也很重要，不加这个很多属性不能再XML里面定义
        this(context, attrs, android.R.attr.editTextStyle);
    }

    public PassWordEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.mContext = context;
        init();
    }

    private OnVisibilityChangedListener onVisibilityChangedListener;

    public interface OnVisibilityChangedListener {
        /**
         * 可见性回调
         *
         * @param isVisible
         */
        void onVisible(boolean isVisible);
    }

    private void init() {
        // 设置右边眼睛图标的高度和宽度
        setIconStatus(isVisible);
        setTextInVisible();
    }


    /**
     * 因为我们不能直接给EditText设置点击事件，所以我们用记住我们按下的位置来模拟点击事件
     * 当我们按下的位置 在  EditText的宽度 - 图标到控件右边的间距 - 图标的宽度  和
     * EditText的宽度 - 图标到控件右边的间距之间我们就算点击了图标，竖直方向就没有考虑
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            if (getCompoundDrawables()[2] != null) {
                boolean touchable = event.getX() > (getWidth() - getTotalPaddingRight());
                if (touchable) {
                    isVisible = !isVisible;
                    if (isVisible) {
                        setTextVisible();
                    } else {
                        setTextInVisible();
                    }
                    setIconStatus(isVisible);
                    setSelection(getText().toString().length());
                    if (onVisibilityChangedListener != null) {
                        onVisibilityChangedListener.onVisible(isVisible);
                    }
                }
            }
        }

        return super.onTouchEvent(event);
    }

    private void setTextVisible() {
        setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        setTransformationMethod(HideReturnsTransformationMethod.getInstance());
    }

    private void setTextInVisible() {
        setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
        setTransformationMethod(PasswordTransformationMethod.getInstance());
    }

    /**
     * 设置眼睛图标的状态切换，调用setCompoundDrawables为EditText绘制上去
     *
     * @param visible
     */
    protected void setIconStatus(boolean visible) {
        drawable = ContextCompat.getDrawable(mContext, visible ? R.drawable.password_visible : R.drawable.password_invisible);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], drawable, getCompoundDrawables()[3]);
    }

    /**
     * 设置晃动动画
     */
    public void setShakeAnimation() {
        this.startAnimation(shakeAnimation(5));
    }

    /**
     * 晃动动画
     *
     * @param counts 1秒钟晃动多少下
     * @return
     */
    public static Animation shakeAnimation(int counts) {
        Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0);
        translateAnimation.setInterpolator(new CycleInterpolator(counts));
        translateAnimation.setDuration(1000);
        return translateAnimation;
    }

    /**
     * 设置可见性监听
     *
     * @param onVisibilityChangedListener
     */
    public void setOnVisibilityChangedListener(OnVisibilityChangedListener onVisibilityChangedListener) {
        this.onVisibilityChangedListener = onVisibilityChangedListener;
    }
}
